library(marginaleffects)

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA17_ideowhite_prep.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat20.Race.WeekWeight.rdata")

# Equalizing weeks with Nationscape
impdat20.WeekWeight$week2 <- as.numeric(impdat20.WeekWeight$week) + 1
impdat20.WeekWeight$week2[which(impdat20.WeekWeight$week2 == 53)] <-  52
impdat20.WeekWeight$week2 <- as.character(impdat20.WeekWeight$week2)
impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)] <- paste0("0", impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)])
impdat20.WeekWeight <- fastDummies::dummy_cols(impdat20.WeekWeight, select_columns = "week2")

impdat20.WeekWeight$week2_post <- "Pre-GF"
for(i in 22:52){
  impdat20.WeekWeight$week2_post[which(impdat20.WeekWeight[, paste0("week2_", i)] == 1)] <- paste("Week", i)
}

# Models ---------------------------------------------------------
# full pre-post
m1_ideo <- lm(D_biep.White_Good_all ~ post_gf*ideo3_lab  +
                age_cat4 + edu_cat4 + sex + 
                census_region + broughtwebsite2,
              data = impdat20.WeekWeight, weights = WeekWeights, subset = race5 == "White")


# Weekly after
m1_pre_weeklypost_ideo <- lm(D_biep.White_Good_all ~ week2_post*ideo3_lab  +
                               age_cat4 + edu_cat4 + sex + 
                               census_region + broughtwebsite2,
                             data = impdat20.WeekWeight, weights = WeekWeights, subset = race5 == "White")

# Plot --------------------------------------------------------------------
# pre-post full
d <- plot_cme(m1_ideo, "post_gf", "ideo3_lab", draw = F)
prepost <- data.frame(ideo = c("White Liberals", "White Conservatives", "White Neutrals\n(Moderates)"),
                      coef = d$estimate,
                      se = d$std.error)


# pre weekly post
dd <- plot_cme(m1_pre_weeklypost_ideo, "week2_post", "ideo3_lab", draw = F)
dd_lib <- subset(dd, ideo3_lab == "liberal")
dd_con <- subset(dd, ideo3_lab == "conservative")
dd_mod <- subset(dd, ideo3_lab == "moderate")
weekly_outs <- data.frame(ideo = c(rep("White Liberals", nrow(dd_lib)),
                                   rep("White Conservatives", nrow(dd_con)),
                                   rep("White Neutrals\n(Moderates)", nrow(dd_mod))),
                          week = c(gsub(" - Pre-GF", "", dd_lib$contrast),
                                   gsub(" - Pre-GF", "", dd_con$contrast),
                                   gsub(" - Pre-GF", "", dd_mod$contrast)),
                          coef = c(dd_lib$estimate, dd_con$estimate, dd_mod$estimate),
                          se = c(dd_lib$std.error, dd_con$std.error, dd_mod$std.error))

# Combine to plot
prepost <- subset(prepost, select = c("ideo", "coef", "se"))
prepost$week <- "Post GF, May 26, 2020-Dec 31, 2020"

coef_out <- rbind(prepost, weekly_outs)

coef_out$ideo <- factor(coef_out$ideo,
                        levels = c("White Liberals", "White Neutrals\n(Moderates)", "White Conservatives"))
coef_out$var <- as.factor(coef_out$week)
coef_out$var <- factor(coef_out$var,
                       levels = rev(levels(coef_out$var)))
coef_out$indx <- c(1, 1, 1, rep(0, nrow(weekly_outs)))
coef_out$indx <- ifelse(coef_out$indx == 1, "Pre-Post", "Weekly Indicators")

coef_out_ideo <- coef_out

# In-Text Summaries -------------------------------------------------------
# liberal effect size
prepost$coef[which(prepost$ideo == "White Liberals")]/sd(impdat20.WeekWeight$D_biep.White_Good_all[which(impdat20.WeekWeight$race5 == "White")],na.rm = T)
# neutral
prepost$coef[which(prepost$ideo == "White Neutrals\n(Moderates)")]/sd(impdat20.WeekWeight$D_biep.White_Good_all[which(impdat20.WeekWeight$race5 == "White")],na.rm = T)
# conservative
prepost$coef[which(prepost$ideo == "White Conservatives")]/sd(impdat20.WeekWeight$D_biep.White_Good_all[which(impdat20.WeekWeight$race5 == "White")],na.rm = T)

# print results for hypothesis test
summary(m1_ideo)

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()